package top.wshape1.demo2.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2024-01-24
 */

@Configuration
@EnableWebSocket    // 使用注解开启WebSocket功能
// 实现WebSocketConfigurer接口并重写registerWebSocketHandlers方法
public class WebSocketConfig implements WebSocketConfigurer {

    @Bean
    public MyWebSocketHandler myWebSocketHandler() {
        return new MyWebSocketHandler();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                // 注册WebSocketHandler ，/websocket是通讯的路径，例如ws://127.0.0.1:8080/websocket
                .addHandler(myWebSocketHandler(), "/websocket")
                // 可以设置拦截器 它们会在 WebSocket握手请求的时候生效
//                .addInterceptors()
                // 跨域设置
                .setAllowedOriginPatterns("*");

    }
}
